package com.moonfish.base.controller;

import com.moonfish.base.controller.Response.BaseResponseBody;
import com.moonfish.base.controller.Response.ErrCode;
import com.moonfish.base.controller.request.RoleRequestBody;
import com.moonfish.base.model.security.Role;
import com.moonfish.base.repository.RoleRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by mayu on 2017/7/11.
 * email:showmara@qq.com
 */
@RestController
@RequestMapping("/api/roles")
@Api(value = "角色管理",description = "角色增删改查")
public class RoleRestController {
    @Autowired
    RoleRepository roleRepository;

    @ApiOperation(value = "增加新角色",notes = "添加新角色",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(value = "令牌",name = "Authorization",required = true,paramType = "header",dataType = "String")
    @Transactional
    @RequestMapping(method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BaseResponseBody> addRole(@ApiParam(name = "角色实体")@RequestBody RoleRequestBody roleRequestBody) {
        Role role = new Role();
        role.setName(roleRequestBody.getName());
        role = roleRepository.save(role);
        if (role != null) {
            return ResponseEntity.ok(new BaseResponseBody(ErrCode.OK, "", "", null));
        } else {
            return ResponseEntity.ok(new BaseResponseBody(ErrCode.EXCEPTION, "", "", null));
        }
    }
    @ApiOperation(value = "删除角色",notes = "删除角色",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(value = "令牌",name = "Authorization",required = true,paramType = "header",dataType = "String")    @Transactional
    @RequestMapping(method = RequestMethod.DELETE)
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BaseResponseBody> deleteRole(@RequestBody RoleRequestBody roleRequestBody) {
        if (roleRepository.findById(roleRequestBody.getId()) == null) {
            return ResponseEntity.ok(new BaseResponseBody(ErrCode.EXCEPTION, "无角色ID", "", ""));
        }
        roleRepository.delete(roleRepository.findById(roleRequestBody.getId()));
        return ResponseEntity.ok(new BaseResponseBody(ErrCode.OK, "", "", ""));
    }
    @ApiOperation(value = "修改角色",notes = "修改角色",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(value = "令牌",name = "Authorization",required = true,paramType = "header",dataType = "String")    @Transactional
    @RequestMapping(method = RequestMethod.PUT)
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BaseResponseBody> updateRole(@RequestBody RoleRequestBody roleRequestBody) {
        Role role = new Role();
        if (roleRepository.findById(role.getId()) == null) {
            return ResponseEntity.ok(new BaseResponseBody(ErrCode.EXCEPTION, "无角色ID", "", ""));
        }
        role.setId(roleRequestBody.getId());
        role.setName(roleRequestBody.getName());
        roleRepository.save(role);
        return ResponseEntity.ok(new BaseResponseBody(ErrCode.OK, "", "", ""));
    }

    @ApiOperation(value = "获取角色",notes = "获取角色",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(value = "令牌",name = "Authorization",required = true,paramType = "header",dataType = "String")    @Transactional
    @RequestMapping(method = RequestMethod.GET)
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BaseResponseBody<List<Role>>> getRole() {
        List<Role> roles;
        roles = roleRepository.findAll();
        return ResponseEntity.ok(new BaseResponseBody(ErrCode.OK, "", "", roles));
    }
}
